﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.Scheduling;
using Domain.Scheduling.Entities;
using Infrastructure.Common.Data;
using System.Data;
using System.Linq.Expressions;

namespace Infrastructure.Data.Scheduling.Repositories
{
    
    public class ScheduleStudyRepository : Repository<ScheduleStudy>, IScheduleStudyRepository
    {

        public ScheduleStudyRepository(IContext context)
            : base(context)
        { }

        public override ScheduleStudy Add(ScheduleStudy item)
        {
            return this.Context.ScheduleStudys.Add(item);
        }

        public override void Update(ScheduleStudy item)
        {
            //this.Context.Skills.Attach(item);
            var entry = this.Context.Entry<ScheduleStudy>(item);// = System.Data.EntityState.Modified;
            entry.Entity.StudyName = item.StudyName;
            
            this.Context.ChangeTracker.DetectChanges();
            //entry.State = System.Data.EntityState.Modified;
            //this.Context.Entry(item).State = System.Data.EntityState.Modified;
        }

        public override void Delete(ScheduleStudy item)
        {
            this.Context.Entry(item).State = System.Data.EntityState.Deleted;
        }

        public override IEnumerable<ScheduleStudy> Get(params Expression<Func<ScheduleStudy, bool>>[] predicates)
        {
            var query = from s in this.Context.ScheduleStudys
                        select s;

            if (predicates != null)
            {
                foreach (var condition in predicates)
                {
                    query = query.Where(condition);
                }
            }

            return query.ToArray();
        }
    }
}
